package com.example.wangzh.mytest.view.path_animator;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class AnimatorPath {
    public List<PathPoint> mPathPonit = new ArrayList<>();
    private View view;

    public AnimatorPath() {
        mPathPonit.add(new PathPoint(PathPoint.MOVE, 0, 0));
    }

    public void cubicTo(float x, float y, float c0X, float c0Y, float c1X, float c1Y) {
        mPathPonit.add(new PathPoint(PathPoint.CUBIC, x, y, c0X, c0Y, c1X, c1Y));
    }

    public void lineTo(float x, float y) {
        mPathPonit.add(new PathPoint(PathPoint.LINE, x, y));
    }

    public void moveTo(float x, float y) {
        mPathPonit.add(new PathPoint(PathPoint.MOVE, x, y));
    }

    public void startAnimation(View v, long duration) {
        view = v;
        //让View动起来，属性动画
//        ValueAnimator.ofFloat();
//        ObjectAnimator.ofFloat();
        //属性动画：反射setTranslationX()方法,setAlpha()方法等等
        //属性动画本质：通过反射控制一个对象的任意属性值使其不断变化
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "pointPosition", 0, 100);
        //让view的x,y位置按照设定的路径进行估值--使用估值器
        ObjectAnimator objectAnimator = ObjectAnimator.ofObject(this, "pointPosition", new PathEvaluate(), mPathPonit.toArray());
        objectAnimator.setDuration(duration);
        objectAnimator.start();
    }

    public void setPointPosition(PathPoint pathPoint) {
        view.setTranslationX(pathPoint.getmX());
        view.setTranslationY(pathPoint.getmY());
    }

    public void clear() {
        mPathPonit.clear();
        mPathPonit.add(new PathPoint(PathPoint.MOVE, 0, 0));
    }

}
